package com.pudu.mydemo.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.WindowManager;

/**
 * 封装系统亮度
 * @author why
 */
public class SystemBrightManager {

	// 判断是否开启了自动亮度调节
	public static boolean isAutoBrightness(Activity activity) {
		boolean autoBrightness = false;
		ContentResolver contentResolver = activity.getContentResolver();
		try {
			autoBrightness = Settings.System.getInt(contentResolver,
					Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
		return autoBrightness;
	}

	// 获取当前系统亮度值
	public static int getBrightness(Activity activity) {
		int brightValue = 0;
		ContentResolver contentResolver = activity.getContentResolver();
		try {
			brightValue = Settings.System.getInt(contentResolver, Settings.System.SCREEN_BRIGHTNESS);
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}
		return brightValue;
	}

	// 改变屏幕亮度
	public static void setBrightness(Activity activity, int brightValue) {
		WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
		lp.screenBrightness = (brightValue <= 0 ? -1.0f : brightValue / 255f);
		activity.getWindow().setAttributes(lp);
	}

	public static void setSystemBrightness(Activity activity,int systemBrightness) {
		Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS,systemBrightness);
	}

	public static int getSystemBrightness(Activity activity) {
		int systemBrightness = 255;
		try {
			 systemBrightness = Settings.System.getInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS);
		} catch (SettingNotFoundException e) {
			e.printStackTrace();
		}

		return systemBrightness;
	}
	// 开启亮度自动亮度模式
	public static void startAutoBrightness(Activity activity) {
		Settings.System.putInt(activity.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
		Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");
		activity.getContentResolver().notifyChange(uri, null);
	}

	// 停止自动亮度模式
	public static void stopAutoBrightness(Activity activity) {
		Settings.System.putInt(activity.getContentResolver(),
				Settings.System.SCREEN_BRIGHTNESS_MODE,
				Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
		Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");
		activity.getContentResolver().notifyChange(uri, null);
	}

	/**
	 * 设置当前屏幕亮度的模式
	 * SCREEN_BRIGHTNESS_MODE_AUTOMATIC=1 为自动调节屏幕亮度
	 * SCREEN_BRIGHTNESS_MODE_MANUAL=0 为手动调节屏幕亮度
	 */
	public static void setBrightnessMode(Activity activity, int brightMode) {
		Settings.System.putInt(activity.getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE, brightMode);
	}
}
